#!/usr/bin/env python
import sys
import os

# Get the file to scan. 
filename = sys.argv[1];
if not os.path.isfile(filename):
  print "Cannot find file.";

if not os.path.splitext(filename)[1] == '.tab':
  print "WARNING: Expecting an Orange formatted data file. You supplied a file without a .tab extension," \
        "which is okay, but make sure the file is Orange format.";

# Open and scan. 
f = open(filename,'r');

# How many variables are there? 
header = f.readline().split('\t');
header[-1] = header[-1].strip(); # remove newline from last element
vars = len(header);

# Kill the next two lines, they're useless. 
f.readline();
f.readline();

# Now, collect the arity and states info. 
aritydata = [set() for i in xrange(vars)];
for line in f: 
  elements = line.split('\t');
  elements[-1] = elements[-1].strip();
  for i in xrange(len(elements)):
    aritydata[i].add(elements[i]);

# Close.
f.close();

# Print out the information. 
print "FORMAT: ";
print "(Variable): <number of states> [state1] [state2] ... [state n]";
print "";

count = 0;
for s in aritydata:
  infoline = header[count] + ": " + str(len(s));
  for state in s: 
    infoline += " " + state;
  print infoline;
  count += 1

